package cn.fxl.user.controller;

import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.user.domain.Logininfo;
import cn.fxl.user.query.LogininfoQuery;
import cn.fxl.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/logininfo")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
public class LogininfoController {
    @Autowired
    private ILogininfoService iLogininfoService;

    @GetMapping
    @ApiOperation("获取所有员工信息")
    public List<Logininfo> getAll() {
        return iLogininfoService.getAll();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取一个员工信息")
    public Logininfo getOne(@PathVariable("id") Long id) {
        return iLogininfoService.getOne(id);
    }

    @PostMapping
    @ApiOperation("高级查询分页查询")
    public GmResult<Logininfo> querPage(@RequestBody LogininfoQuery logininfoQuery) {
        return iLogininfoService.Pagequery(logininfoQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除员工")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            iLogininfoService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "删除失败");
        }
    }

    @PutMapping
    @ApiOperation("新增或修改有id为修改，没用id为新增")
    public AjaxResult saveAndUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null) {
                iLogininfoService.save(logininfo);
            } else {
                iLogininfoService.update(logininfo);
            }
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
    }
    @PatchMapping
    public AjaxResult pathchdel(@RequestBody Long[] a){
        try {
            iLogininfoService.pathchdel(a);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }
}
